package com.eshealthy.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.eshealthy.entity.Drug;
import com.eshealthy.entity.MedicalDevice;
import org.apache.http.HttpHost;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.core.CountRequest;
import org.elasticsearch.client.core.CountResponse;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class EsDevice {
    public List<MedicalDevice>  medicalDeviceList(Integer page, Integer rows) throws IOException {
        HttpHost host=new HttpHost("192.168.127.128",9200,"http");
        //创建es的客户端
        RestClientBuilder restClientBuilder= RestClient.builder(host);
        //获取 elasticsearch-rest-high-level-client
        RestHighLevelClient esClient=new RestHighLevelClient(restClientBuilder);
        //3，构建QueryBuilder对象指定查询方式和查询条件

//      //4,将QuseryBuilder对象设置到SearchSourceBuilder对象中

        //1，构建SearchRequest请求对象，指定索引库
        SearchRequest searchRequest = new SearchRequest();
        searchRequest.indices("medical_device");
        //2,构建SearchSourceBuilder查询对象
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(QueryBuilders.matchAllQuery());

        //分页
        if(page!=0){page=(page-1)*4;}
        System.out.println(page);
        searchSourceBuilder.from(page);
        searchSourceBuilder.size(rows);

        //5，将SearchSourceBuilder设置到SearchRequest中
        searchRequest.source(searchSourceBuilder);
        // 3.查询
        SearchResponse searchResponse = esClient.search(searchRequest,RequestOptions.DEFAULT);
//      System.out.println(searchResponse.toString().replace(" ",""));
        List<MedicalDevice> medicalDeviceList=new ArrayList<>();
        JSONObject jsonObject = JSON.parseObject(searchResponse.toString().replace(" ",""));
        JSONObject json = JSON.parseObject(jsonObject.getString("hits").toString());
//        System.out.println(json);
        JSONArray jsonArray= JSON.parseArray(json.getString("hits"));
        for (int i=0;i<jsonArray.size();i++){
//        System.out.println(jsonArray.getJSONObject(i).get("_source"));
            MedicalDevice medicalDevice=JSONObject.parseObject(jsonArray.getJSONObject(i).get("_source").toString(),MedicalDevice.class);
            medicalDeviceList.add(medicalDevice);
        }

        return medicalDeviceList;
    }
    public static void addDevice(String name,String telspecification,String directions,String useage) throws IOException {
        HttpHost host=new HttpHost("192.168.127.128",9200,"http");
        //创建es的客户端
        RestClientBuilder restClientBuilder= RestClient.builder(host);
        //获取 elasticsearch-rest-high-level-client
        RestHighLevelClient esClient=new RestHighLevelClient(restClientBuilder);
        IndexRequest indexRequest = new IndexRequest("medical_device");
        CountRequest countRequest=new CountRequest("medical_device");
        CountResponse count = esClient.count(countRequest, RequestOptions.DEFAULT);
        String id=String.valueOf(count.getCount()+1);
        //设置文档ID,不设置则系统生成
        indexRequest.id(id);
        Map<String, Object> jsonMap = new HashMap<>();
        jsonMap.put("id", count.getCount()+1);
        jsonMap.put("name", name);
        jsonMap.put("telspecification", telspecification);
        jsonMap.put("directions", directions);
        jsonMap.put("useage", useage);
        indexRequest.source(jsonMap);
        IndexResponse index = esClient.index(indexRequest ,RequestOptions.DEFAULT);
        System.out.println(index);
    }
    public long count()throws IOException{
        HttpHost host=new HttpHost("192.168.127.128",9200,"http");
        //创建es的客户端
        RestClientBuilder restClientBuilder= RestClient.builder(host);
        //获取 elasticsearch-rest-high-level-client
        RestHighLevelClient esClient=new RestHighLevelClient(restClientBuilder);
        //查询数据
        GetRequest request =new GetRequest();
        //查询的索引和id
        CountRequest countRequest=new CountRequest("medical_device");
        CountResponse count = esClient.count(countRequest, RequestOptions.DEFAULT);
        return count.getCount();
    }

}
